

function getQ() {
	var url=window.location.href
	let search = url.substring(url.lastIndexOf('?') + 1);
	let query = {};
	let reg = /([^?&=]+)=([^?&=]*)/g;
	search.replace(reg, (rs, $1, $2) => {
		let name = decodeURIComponent($1);
		let val = decodeURIComponent($2);
		val = String(val);
		query[name] = val;
		return rs;
	});
	return query;
}
function getClassList(){
	var param=getQ()
	var action=param.action;
	if (action==="PUT"){
		var setData=JSON.parse(param.data)
		var selectedClassNumber=setData.classnumber_id;
	}
	jQuery.ajax({
		"url":"/get_class_admin/",
		"error":errorAjax,
		"async":false,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"dataType":"jsonp",
		"success":function(data){
			if(!data.status){
				showErrorInfo(data.content);
				return false;
			}
			else{
				var data=data.content;
				for (var i=0;i<data.length;i++){
					var className=document.getElementById("className");
					var option=document.createElement("option");
					option.textContent=data[i].name
					option.setAttribute("value",data[i].sid)
					className.appendChild(option)
				}
				if (action==="PUT"){
					var test=document.getElementById("test");
					test.value=setData.test_name;
					document.getElementById("description").value=setData.description;
					test.setAttribute("sid",setData.testlist_id);
					test.setAttribute("name",setData.test_name)
					    $("#className").find("option[value='"+ selectedClassNumber + "']").attr("selected",true);
				}
			}
		}

	})
}
document.getElementById("test").onclick=function(){
        window.open ("search_exp.html", "_blank", "height=700, width=1100, top=160, left=200, toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
}
function publishClass(){
	var param=getQ()
	action=param.action
	var data={}
	if (action==="PUT"){
		 data.id=JSON.parse(param.data).id
	}
	data.class_number=$("#className").val();
	data.test_id=document.getElementById("test").getAttribute("sid")
	data.description=document.getElementById("description").value;
	if (data.class_number===undefined || data.test==="null"){
		showErrorInfo("上述内容不能为空！")
		return false;
	}
	data=JSON.stringify(data)
	jQuery.ajax({
		"url":"/publish_test/",
		"error":errorAjax,
		"beforeSend":start_load_pic,
		"complete":stop_load_pic,
		"type":action,
		"data":{"data":data},
		"success":function(data){
			data=JSON.parse(data)
			if(!data.status){
				showErrorInfo(data.status)
				return false;
			}
			else{
				window.opener.$("#showMainContent").load("../html/experimental_job.html");
				alert("操作成功！")
				window.close()
			}
		}
	})
		
}
document.getElementById("confirm").onclick=function(){
	publishClass()
}
$(function(){
getClassList();
})
